package com.xayy.mgt.controller;

import cn.kawins.base.result.ResultBody;
import cn.kawins.mybatis.base.BaseController;
import cn.kawins.spring.mvc.validator.Group;
import com.xayy.mgt.model.BayesHealth;
import com.xayy.mgt.model.BayesHealthRecord;
import com.xayy.mgt.model.NodeTreeInfo;
import com.xayy.mgt.model.vo.*;
import com.xayy.mgt.service.BayesHealthService;
import com.xayy.mgt.service.NodeInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Nie
 * @version V1.0
 * @date 2023/9/19 10:38
 */
@RestController
@RequestMapping(value = "/health/bayes1")
@Api(tags = "贝叶斯评估健康度信息")
public class BayesHealthController extends BaseController<BayesHealth> {

    @Autowired
    private BayesHealthService bayesHealthService;

    @Autowired
    private NodeInfoService nodeInfoService;

    @GetMapping("/getNodeHealthInfo/{satelliteId}/{nodeId}")
    @ApiOperation(value = "获取节点健康度信息", httpMethod = "GET",response = BayesHealthVo.class)
    public ResultBody getNodeHealthInfo(@PathVariable Long satelliteId, @PathVariable Long nodeId) {
        return bayesHealthService.getNodeHealthInfo(satelliteId, nodeId).body();
    }

    @PostMapping("/updateInfo")
    @ApiOperation(value = "修改权重信息", httpMethod = "POST")
    public ResultBody updateInfo(@RequestBody BayesHealth bayesHealth) {
        return bayesHealthService.updateInfo(bayesHealth).body();
    }

    @PostMapping("/getRecordInfo")
    @ApiOperation(value = "获取评估历史信息分页", httpMethod = "POST",response = BayesHealthRecord.class)
    public ResultBody getRecordInfo(@RequestBody HealthRecordVo healthRecordVo) {
        return bayesHealthService.getRecordInfo(healthRecordVo).body();
    }

    @GetMapping("/getRecordInfoById{id}")
    @ApiOperation(value = "获取评估历史详情", httpMethod = "GET",response = BayesHealthRecordVo.class)
    public ResultBody getRecordInfoById(@PathVariable Long id) {
        return bayesHealthService.getRecordInfoById(id).body();
    }

    @GetMapping("/getChildNode/{parentNodeId}")
    @ApiOperation(value = "贝叶斯评估健康度信息-获取所有子节点", httpMethod = "GET", notes = "贝叶斯评估健康度信息-获取所有子节点",response = ComponentAssessVo.class)
    public ResultBody getChildNode(@PathVariable("parentNodeId") Long parentNodeId) {
        List<NodeTreeInfo> select = nodeInfoService.getChildNode(parentNodeId);
        ComponentAssessVo vo = ComponentAssessVo.builder().build();
        List<ComponentAssessVo.Weight> weightList = new ArrayList<>();
        select.forEach(item ->{
            ComponentAssessVo.Weight weight = new ComponentAssessVo.Weight();
            weight.setKey(item.getNodeName());
            weight.setValue(0D);
            weightList.add(weight);
        });
        vo.setWeight(weightList);
        return ResultBody.success(vo);
    }


    @PostMapping("/componentAssess")
    @ApiOperation(value = "贝叶斯评估健康度信息-部件评估", httpMethod = "POST",response = BayesHealthRecordVo.class,notes = "贝叶斯评估健康度信息-部件评估")
    public ResultBody componentAssess(@Validated(Group.Insert.class)  @RequestBody ComponentAssessVo assessVo) {
        return bayesHealthService.componentAssess(assessVo).body();
    }

    @PostMapping("/systemAssess")
    @ApiOperation(value = "贝叶斯评估健康度信息-分系统评估", httpMethod = "POST",response = BayesHealthRecordVo.class,notes = "贝叶斯评估健康度信息-分系统评估")
    public ResultBody systemAssess(@Validated(Group.Insert.class) @RequestBody SystemAssessVo systemAssessVo) {
        return bayesHealthService.systemAssess(systemAssessVo).body();
    }

}
